<?php

namespace app\api\controller\v1\user;

use app\common\controller\BaseController;
use think\Controller;
use think\Request;
use app\api\model\NeedQuestion as NeedQuestionModel;
use app\api\model\Language as LanguageModel;

class Need extends BaseController
{
    protected $noNeedLogin = [''];
    /**
     * 我的需求
     */
    public function index()
    {
        $need = NeedQuestionModel::getUserNeed($this->userId);
        $languageCount = count(LanguageModel::getLanguages());
        $needCount = count($need);
        $this->success('', [
            'need' => $need,
            'language_count' => $languageCount,
            'need_count' => $needCount
        ]);
    }

    /**
     * 需求详情
     */
    public function read()
    {
        $needId = $this->notEmpty('need_id');
        $need = NeedQuestionModel::with('language')->find($needId);
        if (!$need) {
            $this->errorMsg(9995);
        }
        $need['title'] = json_decode($need['title'], true);
        $this->success('', compact('need'));
    }

    /**
     * 删除需求
     */
    public function delete()
    {
        $needId = $this->notEmpty('need_id');
        $need = NeedQuestionModel::find($needId);
        if (!$need) {
            $this->errorMsg(9995);
        }
        $res = $need->delete();
        $res ? $this->success() : $this->forbidden();
    }

    /**
     * 需求语种
     */
    public function language()
    {
        $languages = LanguageModel::getNeedLanguages($this->userId);
        $this->success('', compact('languages'));
    }

}
